"""
按周期计算收益率，简单方法
"""
import pandas as pd
class ror_t2:
    def __init__(self,timeperiod=1):
        self.timeperiod=timeperiod+1
        self.data=[]
        self.timekey = None
    def oncalc(self,d,s,timekey=None):
        rst=0
        if pd.isna(d):
            if self.data:
                d = self.data[-1]
            else:
                return rst
        if timekey != None:
            if timekey == self.timekey:
                self.data[-1] = d
            else:
                self.data.append(d)
                self.timekey = timekey
        else:
            self.data.append(d)
        if len(self.data)>self.timeperiod:
            self.data.pop(0)
        if len(self.data)==self.timeperiod:
            if not pd.isna(s):
                if self.data[0]!=0:
                    rst=s/self.data[0]
        return rst

